package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.AdminUser;
import com.leoman.service.AdminUserService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 管理员管理
 * Created by 涂奕恒 on 2016/10/21.
 */
@Controller
@RequestMapping(value = "backend/adminUser")
public class AdminUserController extends CommonController {

    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping("index")
    public String index() {
        return "backend/管理员列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String account,
                     String mobile,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<AdminUser> page = adminUserService.pageByParams(account, mobile, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer adminUserId) {
        if (null != adminUserId) {
            AdminUser adminUser = adminUserService.getById(adminUserId);
            model.addAttribute("adminUser", adminUser);
        }

        return "backend/新增管理人员";
    }

    /**
     * 新增 or 修改管理员信息
     *
     * @param adminUserId 管理员ID
     * @param account     账号
     * @param mobile      手机号
     * @param password    密码
     * @return 返回管理员ID
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(Integer adminUserId, String account, String mobile, String password) {
        try {
            return adminUserService.saveAdminUser(adminUserId, account, mobile, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除管理员信息
     *
     * @param adminUserId 管理员ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletRequest request, Integer adminUserId) {
        try {
            return adminUserService.deleteAdminUser(request, adminUserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 重置管理员密码
     *
     * @param adminUserId 管理员ID
     */
    @RequestMapping("/resetPwd")
    @ResponseBody
    public Integer resetPwd(HttpServletRequest request, Integer adminUserId) {
        try {
            return adminUserService.reSetAdminUserPassword(request, adminUserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除管理员信息
     *
     * @param ids 管理员ID
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Integer batchDel(HttpServletRequest request, String ids) {
        try {
            Integer[] arrayId = JsonUtil.json2Obj(ids, Integer[].class);
            return adminUserService.batchDeleteAdminUser(request, arrayId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
